//
//  AccountPrecdite.m
//  HomeFurnishingDecoration
//
//  Created by 李劲成 on 16/5/23.
//  Copyright © 2016年 ljc. All rights reserved.
//

#import "AccountPrecdite.h"

@interface AccountPrecdite ()

@end

@implementation AccountPrecdite

+(BOOL)filterUser:(NSString *)userName completionHandle:(void (^)(UsernameError))completion{
    NSLog(@"%@", userName);
    /** 判断用户名是否输入 */
    if (userName.length == 0) {
        
        completion(UsernameNil);
        return NO;
    }
    
    else if (![AccountPrecdite forbiddenCharacter:userName]) {
        completion(UsernameIllegal);
        return NO;
    }
    return YES;
}

+(BOOL)filterPassword:(NSString *)password repeatedPassword:(NSString *)repeatedPassword completionHandle:(void (^)(PasswordError))completion{
  
    
    if (password.length == 0) {
        completion(PasswordNil);
        return NO;
    }
    else if(![AccountPrecdite passwordNumberLength:password]){
        completion(PasswordIllegal);
        return NO;
    }
    else if(repeatedPassword) {
        if (![repeatedPassword isEqualToString:password]) {
            completion(PasswordsIncompatible);
            return NO;
        }
        return YES;
    }
    return YES;
}

// 手机号
+ (BOOL)validateNumber:(NSString *) textString
{
    NSString* number=@"^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\\d{8}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:textString];
}

// 密码
+ (BOOL)passwordNumberLength:(NSString *)textString {
    NSString* number=@"^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,10}$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:textString];
}

// 非法字符
+ (BOOL)forbiddenCharacter:(NSString *)textString {
    NSString* number=@"^\\w+$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:textString];
}

void presentAlert(UIViewController *vc, NSString *title, NSString *message){
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [vc presentViewController:alert animated:YES completion:^{
       
    }];
    [vc performSelector:@selector(dismiss:) withObject:alert afterDelay:1];
}

@end
